/**
 *
 * Copyright 2016-present authing
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import React from 'react';
import { connect } from 'react-redux';
import CodePush from 'react-native-code-push';
import { bindActionCreators } from 'redux';

import { createIconSetFromIcoMoon } from 'react-native-vector-icons';
import icoMoonConfig from '../config/icoMoonConfig.json';
const Icon = createIconSetFromIcoMoon(icoMoonConfig);

import Main from '../pages/MainPage/Main';
import * as authCreators from '../actions/auth';
import * as teamCreators from '../actions/team';
import SCRMWebViewPage from '../pages/ItemDetail/SCRMWebViewPage';
import Badge from '../components/Badge';
import { MSG_LIST_ROUTE } from '../constants/VueRoutes';
import { Text, TouchableOpacity, StyleSheet } from 'react-native';

class MsgContainer extends React.Component {
  constructor(props) {
    super(props);
    console.log('msgContainer', props)
    this.state = {
      article: {
        "url": ''
      }
    }
  }

  static navigationOptions = ({ navigation }) => {
    const { params } = navigation.state;
    const count = params && params.count ? params.count : '';
    // console.log('msgContainer', JSON.stringify(params))
    return {
      title: '智能助手',
      tabBarIcon: ({ tintColor }) => (
        <Badge show={true}>
          <Icon name="h5marketing_n" size={24} color={tintColor} />
        </Badge>
      ),
      headerRight: (
        <TouchableOpacity 
          style={styles.refreshStyle}
          onPress={() => {
            navigation.state.params.handleRefresh && navigation.state.params.handleRefresh();
            // this.webview && this.webview.reload()
        }}>
          <Text
          style={styles.refreshTextStyle}
          >刷新</Text>
        </TouchableOpacity>
      )
    }
  };

  componentWillReceiveProps(nextProps) { 
    console.log('msg container nextProps', nextProps, nextProps.msg)
  }

  componentDidMount() {
    let { params } = this.props.navigation.state; 
    if (!params || !params.article) {
      this.initParams();
    }
  }

  async initParams () {
    let msgLink = await MSG_LIST_ROUTE();
    // console.log(msgLink, 'msgLink')
    this.setState({
      article: {
        "url": msgLink,
        "title": "",
      }
    })
  }

  render() {
    let props = Object.assign({}, (this.props || {}), this.state);
    return <SCRMWebViewPage
    {...props} />;
  }
}

const mapStateToProps = (state) => {
  const { auth, team, msg } = state;
  return {
    auth,
    team, 
    msg
  };
};

const mapDispatchToProps = (dispatch) => {
  const authActions = bindActionCreators(authCreators, dispatch);
  const teamActions = bindActionCreators(teamCreators, dispatch); // test code

  return {
    authActions,
    teamActions // test code
  };
};

const styles = StyleSheet.create({
  refreshStyle: {
    padding: 4
  },
  refreshTextStyle: {
    marginRight: 4,
    color: 'white',
    fontSize: 16
  }
});


export default connect(mapStateToProps, mapDispatchToProps)(MsgContainer);
